package 链表;

public class Demo07 {
    /*
     * 首先想到的是双指针 - 第一次解 不够优雅需要分多钟情况进行讨论
     * */
    static class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }

            ListNode cur = head;
            ListNode nextNode = head.next;
            ListNode pre = null;

            while (nextNode != null) {
                if (cur.val == nextNode.val) {
                    while (nextNode != null && nextNode.val == cur.val) {
                        nextNode = nextNode.next;
                    }
                } else {
                    if (pre == null) {
                        pre = cur;
                        head = pre;
                        continue;
                    } else {
                        pre.next = cur;
                        pre = cur;
                    }
                }
                cur = nextNode;
                if (nextNode != null) nextNode = nextNode.next;
            }
            if (pre == null) return cur;

            pre.next = cur;
            return head;
        }
    }
}
